home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / HyperCard / USING APPS AS XCMDs / xxcmd_shell.c < prev    next >
C/C++ Source or Header  |  1990-06-30  |  3KB  |  144 lines

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <OSUtils.h>
  4. #include <SegLoad.h>
  5. #include <Resources.h>
  6. #include <Files.h>
  7. #include <HyperXCmd.h>
  8. #include "HyperXXCmd.h"
  9.  
  10. #include <stdlib.h>
  11. #include <stddef.h>
  12. #include <string.h>
  13.  
  14. #define ERIK 0x4552494B
  15.  
  16. static short cur_res_wd;
  17. static int argc;
  18. static char *argv[16];
  19.  
  20. main()
  21.  
  22. {
  23.     Str255 apName;
  24.     short apRefNum;
  25.     Handle apParam;
  26.     Size hsize;
  27.     XXCmdPtr xdata;
  28.     int xxcmd_setup();
  29.     short vol;
  30.  
  31.     /* make sure that the application is being called by XXCMD */
  32.     
  33.     GetAppParms(apName, &apRefNum, &apParam);
  34.     hsize = GetHandleSize(apParam);
  35.     xdata = (XXCmdPtr) *apParam;
  36.     if ((hsize == sizeof(XXCmdBlock)) &&
  37.         (xdata->message == 0) &&
  38.         (xdata->count == 0) &&
  39.         (xdata->sig == 0x87654321)) {
  40.         
  41.             /* make all subsequent calls start with xxcmd_setup() */
  42.             
  43.             xdata->nextpc = xxcmd_setup;
  44.             
  45.             /* set the current directory so that files will be found in a resonable place */
  46.             
  47.             xxcmd_get_wd();
  48.             GetVol(nil, &vol);
  49.             SetVol(nil, cur_res_wd);
  50.             
  51.             /* call the initialization routine */
  52.             
  53.             xxcmd_init();
  54.             SetVol(nil, vol);
  55.             
  56.             /* do the normal processing */
  57.             
  58.             xxcmd_setup();
  59.     }
  60. }
  61.  
  62. xxcmd_get_wd()
  63.  
  64. {
  65.     FCBPBRec fcb;
  66.     WDPBRec wd;
  67.     OSErr s;
  68.     
  69.     /* create a working directory for the directory of the current resource file. in */
  70.     /* HyperCard the current resource file will be the current stack. */
  71.     
  72.     fcb.ioCompletion = nil;
  73.     fcb.ioNamePtr = nil;
  74.     fcb.ioVRefNum = 0;
  75.     fcb.ioRefNum = CurResFile();
  76.     fcb.ioFCBIndx = 0;
  77.     if ((s = PBGetFCBInfo(&fcb, false)) == noErr) {
  78.         wd.ioCompletion = nil;
  79.         wd.ioNamePtr = nil;
  80.         wd.ioVRefNum = fcb.ioFCBVRefNum;
  81.         wd.ioWDProcID = ERIK;
  82.         wd.ioWDDirID = fcb.ioFCBParID;
  83.         if ((s = PBOpenWD(&wd, false)) == noErr) {
  84.             cur_res_wd = wd.ioVRefNum;
  85.         } else {
  86.             cur_res_wd = 0;
  87.         }
  88.     } else {
  89.         cur_res_wd = 0;
  90.     }            
  91. }
  92.  
  93. xxcmd_setup()
  94.  
  95. {
  96.     Str255 apName;
  97.     short apRefNum, vol;
  98.     Handle apParam, h;
  99.     XCmdPtr paramPtr;
  100.     char *p, *xxcmd_dispatch();
  101.     int i;
  102.  
  103.     /* set up the parameters to the XXCMD */
  104.     
  105.     GetAppParms(apName, &apRefNum, &apParam);
  106.     paramPtr = ((XXCmdPtr) *apParam)->paramPtr;
  107.     argc = paramPtr->paramCount;
  108.     for (i = 0; i < argc; i++) {
  109.         HLock(paramPtr->params[i]);
  110.         argv[i] = *(paramPtr->params[i]);
  111.     }
  112.     
  113.     /* set up the current directory */
  114.     
  115.     GetVol(nil, &vol);
  116.     SetVol(nil, cur_res_wd);
  117.     
  118.     /* call the processing routine */
  119.     
  120.     p = xxcmd_dispatch(argc, argv);
  121.     
  122.     /* clean up */
  123.     
  124.     SetVol(nil, vol);
  125.     for (i = 0; i < argc; i++) {
  126.         if (argv[i] != NULL) {
  127.             HUnlock(paramPtr->params[i]);
  128.         }
  129.     }
  130.     
  131.     /* set up the return value */
  132.     
  133.     if (p != NULL) {
  134.         if (PtrToHand(p, &h, strlen(p) + 1) == noErr) {
  135.             paramPtr->returnValue = h;
  136.         } else {
  137.             p = "ERROR: out of memory.";
  138.             if (PtrToHand(p, &h, strlen(p) + 1) == noErr) {
  139.                 paramPtr->returnValue = h;
  140.             }
  141.         }
  142.     }
  143.     
  144. }